<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <link rel="stylesheet" href="/js/prism.css">
    <link rel="stylesheet" href="/css/app.css">
    <title>Panda-api test_data匹配说明 - 叫兽</title>
    <link rel="icon" type="image/x-icon" class="js-site-favicon" href="/img/favicon.ico">
  </head>
  <body>

        <div class="container bg-white pb-5 px-5 spx-0">
            
            <h1 class="post-title">Panda-api test_data匹配说明</h1>




            <p>Panda api中， test_data是一个非常非常重要的设计，可以方便前端进行各种情况的测试。</p><p>test_data中，有这么几个字段可以进行数据匹配</p><pre><code class=".language-json5">{
    method: &quot;GET&quot;, // 请求方法匹配
    url:&quot;/post/10/&quot;, // 网址匹配
    query: {}, // query/GET 参数匹配
    body: {}, // post中body参数匹配
    form&#95;data: {}, // post中请求数据在form&#95;data中的参数匹配

    response:{} // 匹配成功后返回的结果
}
</code></pre><p><code>method</code>, <code>url</code>、<code>query</code>、<code>body</code> 或 <code>form&#95;data</code> 这四者是可以同时存在或者只有其中一个存在，或者都不存在， 匹配成功后返回response中的结果</p><h3>使用url匹配的test_data</h3><pre><code class=".language-json5">{
    name:&quot;Article view&quot;,
    method:&quot;GET&quot;,
    url:&quot;/post/{id:\\d+}/&quot;, // the url support regex match
    response:{
        code:{name:&quot;response result code&quot;, type:&quot;int&quot;, desc:&quot;success is 1&quot;},
        msg:{name:&quot;response result message&quot;, type:&quot;string&quot;, desc:&quot;&quot;},
        data: {
            &quot;-name&quot;:&quot;data field name&quot;,
            &quot;-desc&quot;:&quot;data field description&quot;,
            &quot;-required&quot;:false,
            id:{name:&quot;article id&quot;, type:&quot;number&quot;},
            title:{name:&quot;article title&quot;},
            summary:{name:&quot;article summary&quot;},
            content:{name:&quot;article content&quot;},
            category&#95;name:{name:&quot;article category name&quot;},
            author&#95;name:{name:&quot;article author name&quot;},
            source:{name:&quot;article source&quot;},
            tag:{name:&quot;article tag&quot;},
            read&#95;count:{name:&quot;article read count&quot;, type:&quot;number&quot;},
            created:{name:&quot;article created time&quot;, type:&quot;number&quot;},
            }
    },
    test&#95;data:&#91;
        {
            url:&quot;/post/1/&quot;, // use url match
            response:{
                code:1,
                msg:&quot;&quot;,
                data: {
                    id:1,
                    title:&quot;Hello World&quot;,
                    summary:&quot;This is the first post&quot;,
                    content:&quot;This is the first post, and it is a test case data&quot;,
                    category&#95;name:&quot;Other Category&quot;,
                    author&#95;name:&quot;Edison&quot;,
                    source:&quot;Panda api&quot;,
                    tag:&quot;test&quot;,
                    read&#95;count:1,
                    created:1580048711
                }
            },
        },
        {
            url:&quot;/post/3000/&quot;,
            response:{
                code:-1,
                msg:&quot;article not found&quot;
            },
        }
    &#93;
},

</code></pre><p>首先，我们的url是一个正则匹配<code>/post/{id:\\d+}/</code>，<code>/post/&#40;数字&#41;/</code>的都满足匹配条件，</p><p>test_data中的第一个元素，<code>url:&quot;/post/1/&quot;</code>， 必须请求url是<code>/post/1/</code>, 才会返回其对应的 <code>response</code>；</p><p>test_data中的第二个元素，<code>url:&quot;/post/3000/&quot;</code>, 必须请求url是 <code>/post/3000/</code>，才会返回其对应的<code>response</code>；</p><h3>使用query 进行匹配</h3><pre><code class=".language-json5">{
    name:&quot;user logout&quot;,
    method:&quot;GET&quot;,
    url:&quot;/logout/&quot;,
    query:{
        id:{name:&quot;user id&quot;},
        username:{}
    },
    response:{
        code:{name:&quot;response result code&quot;, type:&quot;int&quot;, desc:&quot;success is 1&quot;},
        msg:{name:&quot;response result message&quot;, type:&quot;string&quot;, desc:&quot;&quot;}
    },
    test&#95;data:&#91;
        {
            query:{id:1, username:&quot;root&quot;},
            response:{code:1, msg:&quot;logout success&quot;}
        },
        {
            response:{code:-1, msg:&quot;error&quot;}
        },
        {
            query:{id:3, username:&quot;lily&quot;},
            response:{code:-1, msg:&quot;username and id not match&quot;}
        }
    &#93;
}

</code></pre><h3>使用body进行匹配</h3><pre><code class=".language-json5">{
    name:&quot;user login&quot;,
    desc:&quot;if user login success, will get a token&quot;,
    method: &quot;POST&quot;,
    url:&quot;/login/&quot;,
    body&#95;mode:&quot;json&quot;, // form-data, text, json, html, xml, javascript, binary
    body:{
        username:{name: &quot;username&quot;},
        password:{name: &quot;password&quot;}
    },
    response:{
        code:{name:&quot;response result code&quot;, type:&quot;int&quot;, desc:&quot;success is 1&quot;},
        msg:{name:&quot;response result message&quot;, type:&quot;string&quot;, desc:&quot;&quot;},
        token:{name:&quot;login success, get a user token; login failed, no token&quot;, type:&quot;string&quot;, required:false}
    },
    test&#95;data:&#91;
        {
            body:{username:&quot;edison&quot;, password:&quot;123&quot;},
            response:{code:-1, msg:&quot;password incorrect&quot;}
        },
        {
            body:{username:&quot;lily&quot;, password:&quot;123&quot;},
            response:{code:-2, msg:&quot;username not exist&quot;}
        },
        {
            body:{username:&quot;root&quot;, password:&quot;123&quot;},
            response:{code:1, msg:&quot;login success&quot;, token:&quot;fjdlkfjlafjdlaj3jk2l4j&quot;}
        },
        {
            body:{username:&quot;lily&quot;},
            response:{code:-1, msg:&quot;password is required&quot;}
        },
        {
            body:{password:&quot;123&quot;},
            response:{code:-1, msg:&quot;username is required&quot;}
        }
    &#93;
},
</code></pre><h3>form_data匹配</h3><p>把上个例子中<code>body</code>字段名改为<code>form&#95;data</code>即可。</p><h3>使用method进行匹配</h3><p><code>method</code>可以直接写字符串<code>method:&quot;GET&quot;</code>，单个方法匹配，也可以写数组，一次匹配多种方法，<code>method:&#91;&quot;GET&quot;, &quot;POST&quot;&#93;</code>，也可以匹配全部方法：<code>method:&quot;&#42;&quot;</code></p><p>如果不在test_data中设置<code>method</code>，表示<code>method</code>请求方法默认都是匹配的。</p><pre><code class=".language-json">{
    // 假设在这个接口中，编辑获取数据和提交保存是一个接口，真实开发中，不建议这么做
    name:&quot;Article add&quot;,
    desc:&quot;Article add or edit&quot;,
    method:&#91;&quot;GET&quot;, &quot;POST&quot;&#93;,
    url:&quot;/post/add/&quot;,
    body&#95;mode:&quot;json&quot;, // form-data, text, json, html, xml, javascript, binary
    body:{
        id:{name: &quot;article id&quot;},
        title:{name: &quot;article title&quot;, required:false},
        content:{name: &quot;article content&quot;, required:false}
    },
    response:{
        id: {name: &quot;Article id&quot;, type:&quot;PosInt&quot;},
        title:{name: &quot;article title&quot;},
        content:{name: &quot;article content&quot;}
    },
    test&#95;data:&#91;
        {
            method:&quot;GET&quot;,
            body:{
                id:1
            },
            response: {
                id:1,
                title:&quot;Hello World&quot;,
                content:&quot;Article Content&quot;
            }
        },
        {
            method:&quot;POST&quot;,
            body:{
                id:1,
                title:&quot;Hello World&quot;,
                content:&quot;Article Content&quot;
            },
            response: {
                id:1,
                title:&quot;Hello World&quot;,
                content:&quot;Article Content&quot;
            }
        },
    &#93;
}

</code></pre><h2>增加test_data数据的描述说明</h2><p>可以在<code>test&#95;data</code>的数据列表的元素中，增加<code>name</code>字段和<code>desc</code>字段，进行测试用例数据的描述，<code>name</code>字段和<code>desc</code>字段与<code>method</code>、<code>body</code>这些字段是同级的。增加了<code>name</code>和<code>desc</code>不会影响<code>test&#95;data</code>的匹配，两个字段都是可选的。</p><pre><code class=".language-json5">test&#95;data:&#91;
    {
        name:&quot;编辑文章时获取文章内容&quot;,
        method:&quot;GET&quot;,
        body:{
            id:1
        },
        response: {
            id:1,
            title:&quot;Hello World&quot;,
            content:&quot;Article Content&quot;
        }
    },
    {
        name:&quot;提交文章编辑内容&quot;,
        desc:&quot;提价文章编辑内容后，数据立即更新内容&quot;,
        method:&quot;POST&quot;,
        body:{
            id:1,
            title:&quot;Hello World&quot;,
            content:&quot;Article Content&quot;
        },
        response: {
            id:1,
            title:&quot;Hello World&quot;,
            content:&quot;Article Content&quot;
        }
    },
&#93;
</code></pre>
            <p class="text-left text-muted">2020-02-05 06:37</p>
        </div>
        <div class="container">
            <div class="row mt-4">
                <div class="col-6 text-left"><a href="/p/2020/1/29/Panda-api字段说明/">上一篇: Panda-api字段说明</a></div>
                <div class="col-6 text-right"><a href="/p/2020/1/24/Panda-api中文说明/">下一篇:Panda-api中文说明</a></div>
            </div>
        </div>

        <button class="btn btn-link m-menu-toggle d-md-none fixed-top collapsed" type="button" data-toggle="collapse" data-target="#m-menu" aria-controls="m-menu" aria-expanded="false" aria-label="Toggle docs navigation"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 30 30" width="30" height="30" focusable="false"><title>Menu</title><path stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-miterlimit="10" d="M4 7h22M4 15h22M4 23h22"></path></svg>
        </button>
        <ul class="nav flex-column collapse fixed-top site-menu d-md-block" id="m-menu">
            <li class="nav-item">
                <a class="nav-link" href="/">首页</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/p/list/">所有文章</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="/p/about-me/">关于我</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="https://github.com/arlicle">Github</a>
          </li>
        </ul>

        
    <div class="container mt-5">
        <h3>留言</h3>
        <div id="commentApp"></div>
    </div>
    <script>
        var AL_configs = {
            "post_id":"/p/2020/1/27/Panda-api-test_data匹配说明/",
            "appid":"847e226e8a46f64045d45312245a68ba1368a564"
        };
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://comment.debugmyself.com/sc.js";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>


        <footer class="footer mt-5 pb-2">
        <div class="container text-center">
          
          <p><span class="text-black-50">Powered by <a href="https://github.com/arlicle/ablog">ablog</a> © 2019 叫兽</span></p>
          <p><span class="text-black-50"><a href="http://www.beian.miit.gov.cn/">滇ICP备10201832号-3</a></span></p>
          
        </div>
        </footer>
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
        <script src="/js/prism.js"></script>
        <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip();
        })
        </script>
        <script type="text/x-mathjax-config">
        MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}, displayAlign: "left",scale: 180});
        </script>
        <script type="text/javascript" async
        src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_SVG" async>
        </script>
  </body>
</html>